package com.atcx.service.impl;

import com.atcx.mapper.UserMapper;
import com.atcx.pojo.User;
import com.atcx.service.UserService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    /**
     * @Author:pangshu
     * @Description: 用户注册，或添加用户，
     * @Date:20:58 2022/3/23
     * @Param:
     * @return: int
     **/
    @Override
    public int addUser(User user) {
        //设置职位，权限，注册时间，
        user.setPostion("学生");
        user.setLevel("B");
        user.setLatest(LocalDate.now());
        int insert = userMapper.insert(user);
        return insert;
    }
    /**
     * @Author:pangshu
     * @Description: 通过账户密码查询
     * @Date:21:10 2022/3/23
     * @Param:
     * @return: void
     **/
    @Override
    public User getUserByNP(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername()).eq("password",user.getPassword());
        User userOne = userMapper.selectOne(wrapper);
        return userOne;
    }
    /**
     * @Author:pangshu
     * @Description: 修改用户信息，密码
     * @Date:21:25 2022/3/23
     * @Param:
     * @return: int
     **/
    @Override
    public int updateUser(User user) {
        int i = userMapper.updateById(user);
        return i;
    }

    @Override
    public int deleteUser(int id) {
        int i = userMapper.deleteById(id);
        return i;
    }
    //分页查询
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<User> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (queryPageBean.getQueryString() != null&&queryPageBean.getQueryString().trim().length()!=0){
            //有查询条件就添加查询条件
            String trim = queryPageBean.getQueryString().trim();
            queryWrapper.eq("name",trim);
        }
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(userPage.getTotal(), userPage.getRecords());
        return pageResult;
    }
    /**
     * @Author:pangshu
     * @Description: 当前用户名是否存在，true表示不存在,可以注册；false，存在
     * @Date:13:16 2022/3/25
     * @Param:
     * @return: boolean
     **/
    @Override
    public boolean getNameExist( String name) {
        Boolean flag = true;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        User user = userMapper.selectOne(wrapper);
        if (user!=null){
            flag = false;
        }
        return flag;
    }
    /**
     * @Author:pangshu
     * @Description: true表示不存在,可以注册，false不能注册
     * @Date:13:39 2022/3/25
     * @Param:
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean getUserNameExist(String username) {
        Boolean flag = true;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        if (user!=null){
            flag = false;
        }
        return flag;
    }

    @Override
    public User getUserById(Integer userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public int addUserByManager(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }
}
